<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_51job/model')->方法名();
*/
namespace jy_51job;
defined('BY_JYA') or exit('error');
class merch extends \plugin{
	public function checkmerch($action=''){
		if(!$_SESSION['userid']){
			$user=pdo_get('jy_51job_merch_account',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
			if(!$user['id']){
				exi('请先登录企业账号','error','jy_51job/login/');
			}
		}else{
			$user=pdo_get('jy_51job_merch_account',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['userid']));
			if(!$user['id']){
				exi('请先登录企业账号。','error','jy_51job/login/');
			}
		}
		$_SESSION['userid']=$user['id'];
		$_SESSION['merchid']=$user['merchid'];
		$_SESSION['member']['username']=$user['name'];
		$merchid=pdo_getcolumn('jy_51job_merch',array('id'=>$_SESSION['merchid'],'uniacid'=>$_SESSION['uniacid']),'id');
		if(!$_SESSION['uid']){
			exi('请先登录企业账号！','error','jy_51job/login/');
		}
		if((!$merchid || !$_SESSION['merchid']) && $action !='merch.info'){
			exi('请先完善企业信息！','error','jy_51job/merch.info/');
		}
		return $merchid;
	}
	public function member_show($id,$default_url='',$default=false){		
		$member=pdo_get('jy_51job_merch',array('id'=>intval($id)),array('id','name','logo','mobile'));
		if(!$member){
			$member=$default?:array('nickname'=>'无');
		}else{
			$member['nickname']=$member['usename']?:'无';
			$member['avatar']=tomedia($member['avatar']);
		}
		$member['url']='jy_51job/merch/look/';
		return $member;
	}
	public function jianlilook($merchid=0,$isexi=0){
		$basic=$this->plugin_setting('basic');
		if(!$basic['vipopen']){
			return 0;
		}
		$merchid=$merchid?:$this->checkmerch();
		if(!$merchid){
			if($isexi){
				exi('企业才能查看简历！','error');
			}
			return 1;
		}
		$islooktime=pdo_count('jy_51job_jianli_log',array('islook'=>1,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		if($basic['freelooktime'] && $islooktime<$basic['freelooktime']){
			return 0;
		}
		$merch=pdo_get('jy_51job_merch',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		if(!$merch['taocanid'] || $merch['endtime']<time()){
			if($isexi){
				exi('您的VIP已过期！','error');
			}
			return 1;
		}
		$daytime=mktime(0,0,0,date('m'),date('d'),date('Y'));
		$islooktimeday=pdo_count('jy_51job_jianli_log',array('createtime >'=>$daytime,'islook'=>1,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		if($merch['taocanid'] && $merch['notenum']<=$islooktime){
			if($isexi){
				exi('您的查看简历次数已用完！','error');
			}
			return 2;
		}
		$merch['daynum']=pdo_getcolumn('jy_51job_merch_taocan',array('id'=>$merch['taocanid'],'uniacid'=>$_SESSION['uniacid']),'notenum_day');
		if($merch['taocanid'] && $merch['daynum']<=$islooktimeday){
			if($isexi){
				exi('您今日查看简历次数已用完！','error');
			}
			return 3;
		}
		return 0;
	}
	//errormsg：1、您的免费发布职位次数已用完！2、您的vip时间已到期！3、您的年度发布职位次数已用完！4、您的今日发布职位次数已用完！
	public function jobuptime($merchid=0){
		$basic=$this->plugin_setting('basic');
		if(!$basic['vipopen']){
			return 0;
		}
		$merchid=$merchid?:$this->checkmerch();
		if(!$merchid){
			exi('企业才能发布职位！','error','/merch.index/');
			return 1;
		}
		$isjobtime=pdo_count('jy_51job_job',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		if($basic['freetime'] && $isjobtime>=$basic['freetime']){
			exi('','error','/merch.buyvip//errormsg=1');
			return 0;
		}
		$merch=pdo_get('jy_51job_merch',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		if(!$merch['taocanid'] && $merch['endtime']<time()){
			exi('','error','/merch.buyvip//errormsg=2');
			return 1;
		}
		$daytime=mktime(0,0,0,date('m'),date('d'),date('Y'));
		$isjobtimeday=pdo_count('jy_51job_job',array('createtime >'=>$daytime,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		if($merch['taocanid'] && $merch['jobnum']<=$isjobtime){
			exi('','error','/merch.buyvip//errormsg=3');
			return 2;
		}
		if($merch['taocanid'] && $merch['jobnum_day']<=$isjobtimeday){
			exi('','error','/merch.buyvip//errormsg=4');
			return 3;
		}
		return 0;
	}
}
?>